(function(){

        /**
         * @function 列表项模板
         * @param { string } id 身份证
         * @param { string } phone 手机号 
         * @param { string } name 来访人
         * @param { string } unit 所属单位
         * @param { string } accessTime 来访日期
         * @param { string } statusColor 状态标识颜色
         * @param { string } status 记录状态
         */
        function itemTmp({ id, phone, name, unit, accessTime, statusColor, status }){

            return `
                    <li class="paddingLiB table-view-cell" data-id=${id}>
                    <div class="navigate-right">
                        <div class="row-fluid">
                            <div class="span10">
                                <div class="row-fluid  lineH30">
                                    <div class="span5 f_26 c333"><i class="icon-user f_18 cCCC stringBug1 paddingR10"></i>${name}</div>
                                    <div class="span7 f_22 c666 "><i class="icon-phone f_16 cCCC paddingR10 stringBug1"></i>${phone}</div>
                                </div>
                                <div class="f_18  c999 row-fluid">
                                    <div class="span5 stringBug5">${accessTime}</div>
                                    <div class="span7  ">
                                        <div class="oneIn oneIn_1"><i class="icon-office f_16 cCCC paddingR10 stringBug1"></i>${unit}</div>
                                    </div>
                                </div>
                            </div>
                            <div class="span2">
                                <div class="f_20 text-right" style="padding-top:15px; color: ${statusColor}"> ${ status }</div>
                            </div>
                        </div>
                    </div>
                </li>
                <li class="nothingDo"></li>
            `
        }

    

    app.section.onReady = function (params) {
        $('#_1_txP').remove();
    };
    
    app.section.onLoad = function () {
        var page=0;
        var statusS = $app.getSession('status');
        var keywordS = $app.getSession('keyword');

        if($app.judgeData(keywordS)){
            $('#topText').val(keywordS);
            $app.removeSession('keyword');
        }
        
        //一般上拉、下拉刷新只选择一种使用，下面同时出现两种只为方便演示
        var iScroll = ui.IScroll.init("#_2_pullrefresh", {
            scrollBar: true,
            enablePullDown: false,
            enablePullUp: true,
            pullUpAction: pullUpActionDemo
        });


    
        /**
         * 首次加载, 默认初始状态 "未访"
         */
        statusS = statusS === null ? "20" : statusS
        const currentStatus = $app.VISIT_STATUS(statusS)
        
    
        /**
         * @function 设置状态按钮
         * @param { object } conf 状态类配置
         */
        function setStatus({ label, code, color }){
            $('#zSta').text(label).attr('data-status', code);
            $('#zSta').parents('button').css({
                background: color
            });
        }
        

        /**
         * 设置类型按钮当前状态
         */
        setStatus(currentStatus)
        $app.removeSession('status');

        
        //上拉加载
        function accessList(){

            page += 1;

            $app.loading();
            var keyword=$app.inputVal('#topText');
            var statusCode = currentStatus.code;
            var orderBy = currentStatus.sort;

            
            /**
             * @function 请求数据并构建列表
             */
            $ajax.accessList(statusCode, page, keyword, orderBy, function(res){

                $app.iScrollDo(res,iScroll,function(resData){

                    resData = [...resData,  ...resData]
    
                    var htmlY = resData.reduce((acc, next) => {
                            const { label, color } = $app.VISIT_STATUS(next.status)
                            
                            return acc + $app.searchListCard(Object.assign({}, next, {

                                status: label,
                                statusColor: color,
                                compony: next.unit || ' - ',
                                date: $app.delSecond(next.accessTime)
                                
                            }))

                            // return acc + itemTmp(Object.assign({}, next, {

                            //     status: label,
                            //     statusColor: color,
                            //     unit: next.unit || ' - ',
                            //     accessTime: $app.delSecond(next.accessTime)

                            // }))
    
                        }, '')
    
                    $("#thelist").append(htmlY);//节点插入
    
                });
                
            });
        }
        

        // 初始加载
        accessList();
    
        function pullUpActionDemo(refreshCallback) {
            setTimeout(function () {
                accessList();
                refreshCallback && refreshCallback();
            }, 300);
        }

        //搜索
        $('#searchGo').on('click',function(){
            var keyword=$app.inputVal('#topText');
            $app.setSession(keyword,'keyword');
            ui.refresh();
        });
        
        
    
        $('#btn_actionsheet').tap(function(){

            const statusConf = $app.createVisitStatus(true);
            const menuList = statusConf.map(item => {

                return {
                    color: item.color,
                    text: item.label,
                    handler(){
                        $app.setSession( item._code, 'status' );
                        setTimeout(() => ui.refresh(), 100)
                    }
                }
                
            })

            ui.Popup.actionsheet(menuList);

        });
    


        
        //跳转详情
        $('#thelist').on('tap','li',function(){
    
            ui.load({
                url: '_2_detail.html',
                params: {
                    id: $(this).attr('data-id')
                }
            });
        });
    };


  
    
})(window)